// 单元格格式化
export function cellFormatter(row, column, emptyCellText) {
  const cellValue = column?.formatter ? column.formatter(row, column, row[column.prop]) : row[column.prop]
  return cellValue === null || cellValue === undefined || cellValue === '' ? emptyCellText : cellValue
}

// 行列转换，通常被table-report使用
export function transformReportData({ data, config, thLabelFn, thPropFn }) {
  if (!data || data.length === 0) {
    return {
      data: [],
      config: [],
    }
  }

  const newColumns = []
  // 第一列
  newColumns.push({
    label: config[0].label,
    prop: thPropFn(config[0], 0),
  })
  // 其余列
  data?.length &&
    data.forEach((dataCol, index) => {
      newColumns.push({
        label: thLabelFn(dataCol, index + 1),
        prop: thPropFn(dataCol, index + 1),
      })
    })

  const rightColumns = newColumns.slice(1)
  const bodyConfig = config.slice(1)

  const newData = []
  bodyConfig.forEach((row) => {
    const newRow = { [newColumns[0].prop]: row.label }
    rightColumns.forEach((col, index) => {
      newRow[col.prop] = data[index][row.prop]
    })
    // 附加isRowHeader
    if (row.isRowHeader) newRow.__isRowHeader__ = true
    // 附加labelStyle
    if (row.labelStyle) newRow.__labelStyle__ = row.labelStyle
    // 附加formatter
    if (row.formatter) newRow.formatter = row.formatter
    // 附加labelSlot
    if (row.labelSlot) newRow.labelSlot = row.labelSlot
    // 附加valueSlot
    if (row.valueSlot) newRow.valueSlot = row.valueSlot
    newData.push(newRow)
  })

  return {
    data: newData,
    columns: newColumns,
  }
}
